<template>
  <div id="userManagePage" flex="auto">
    <a-flex justify="space-between" style="margin-bottom: 16px">
      <h2>用户管理</h2>
      <a-space>
        <a-button type="primary" @click="showAddModal">+ 新增用户</a-button>
      </a-space>
    </a-flex>
    <div class="module">
      <!-- 搜索框 -->
      <div class="search">
        <a-form layout="inline" :model="searchParam" @finish="doSearch">
          <a-form-item label="账号">
            <a-input v-model:value="searchParam.userAccount" placeholder="输入账号" allow-clear />
          </a-form-item>
          <a-form-item label="用户名">
            <a-input v-model:value="searchParam.userName" placeholder="输入用户名" allow-clear />
          </a-form-item>
          <a-form-item>
            <a-button type="primary" html-type="submit">搜索</a-button>
          </a-form-item>
        </a-form>
      </div>

      <!-- 新增用户 -->
    </div>

    <!-- 表格 -->
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAvatar'">
          <div v-if="record.userAvatar === null || record.userAvatar === ''">无</div>
          <div v-else>
            <a-image :src="record.userAvatar" :width="80" />
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <div>
            <a-tag color="green" v-if="record.userRole === 'admin'">管理员</a-tag>
            <a-tag color="blue" v-if="record.userRole === 'user'">普通用户</a-tag>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'updateTime'">
          {{ dayjs(record.updateTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <div class="action">
            <!-- 编辑用户 -->
            <div>
              <a-button type="link" @click="showEditModal(record)">编辑 </a-button>
            </div>
            <!-- 删除用户 -->
            <div>
              <a-button type="link" danger @click="showDeleteModal(record.id)">删除</a-button>
            </div>
          </div>
        </template>
      </template>
    </a-table>
  </div>

  <!-- 对话框组件 -->
  <AddUserDialog ref="AddUserDialogRef" @refresh="fetchUserData" />
  <EditUserDialog ref="EditUserDialogRef" @refresh="fetchUserData" />
  <DeleteUserDialog ref="DeleteUserDialogRef" @refresh="fetchUserData" />
</template>
<script lang="ts" setup>
import EditUserDialog from './components/EditUserDialog.vue'
import DeleteUserDialog from '@/pages/admin/components/DeleteUserDialog.vue'
import { computed, onMounted, reactive, ref } from 'vue'
import { listUserVoByPageUsingPost } from '@/api/userController.ts'
import { message, Modal } from 'ant-design-vue'
import dayjs from 'dayjs'
import AddUserDialog from '@/pages/admin/components/AddUserDialog.vue'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    align: 'center',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
    align: 'center',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
    align: 'center',
  },
  {
    title: '头像',
    dataIndex: 'userAvatar',
    align: 'center',
  },
  {
    title: '简介',
    dataIndex: 'userProfile',
    align: 'center',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
    align: 'center',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    align: 'center',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    align: 'center',
  },
  {
    title: '操作',
    key: 'action',
    align: 'center',
  },
]

// 定义数据
const dataList = ref<API.UserVO[]>([])
const total = ref(0)

// 搜索条件
const searchParam = reactive<API.UserQueryRequest>({
  current: 1,
  pageSize: 10,
  sortField: 'createTime',
  sortOrder: 'ascend',
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParam.current,
    pageSize: searchParam.pageSize,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total) => `共${total}条`,
  }
})

const doTableChange = (page: any) => {
  searchParam.current = page.current
  searchParam.pageSize = page.pageSize
  fetchUserData()
}

// 获取用户信息数据
const fetchUserData = async () => {
  const userVoData = await listUserVoByPageUsingPost({
    ...searchParam,
  })
  if (userVoData.data.code === 0 && userVoData.data.data) {
    dataList.value = userVoData.data.data.records ?? []
    total.value = userVoData.data.data.total ?? 0
  } else {
    message.error('获取用户信息失败，' + userVoData.data.message)
  }
}

// 搜索事件
const doSearch = () => {
  searchParam.current = 1
  fetchUserData()
}

// 引入添加用户组件方法
const AddUserDialogRef = ref(null)
// 展示添加用户对话框
const showAddModal = () => {
  // 调用添加用户子组件方法
  AddUserDialogRef.value!.openDialog()
}

// 引入编辑子组件方法
const EditUserDialogRef = ref(null)
// 编辑用户所需参数
const editUserParam = ref<API.UserUpdateRequest>()
// 展示编辑用户对话框
const showEditModal = (record: any) => {
  editUserParam.value = {
    id: record.id,
    userName: record.userName,
    userProfile: record.userProfile,
    userAvatar: record.userAvatar,
    userRole: record.userRole,
  }
  // 调用编辑子组件方法
  EditUserDialogRef.value!.openDialog(editUserParam.value)
}

// 引入删除子组件方法
const DeleteUserDialogRef = ref(null)
// 删除用户所需参数
const deleteId = ref<number>(0)
// 展示删除用户对话框
const showDeleteModal = (id: number) => {
  // 调用删除子组件方法
  DeleteUserDialogRef.value!.openDialog(id)
}

// 钩子函数，页面加载时获取数据
onMounted(() => {
  fetchUserData()
})
</script>

<style scoped>
.module {
  margin-bottom: 16px;
  display: flex;
  justify-content: space-between;
}

.search {
  margin-left: 20px;
}

.add {
  margin-right: 20px;
}

.action {
  display: flex;
  justify-content: space-around;
}
</style>
